package com.ruoyi.develop.lvFahuotongzhi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSaledo.service.IGoodsSaledoService;
import com.ruoyi.develop.lvFahuomingxi.domain.LvFahuomingxi;
import com.ruoyi.develop.lvFahuomingxi.service.ILvFahuomingxiService;
import com.ruoyi.develop.lvYifahuomingxi.domain.LvYifahuomingxi;
import com.ruoyi.develop.lvYifahuomingxi.service.ILvYifahuomingxiService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvFahuotongzhi.domain.LvFahuotongzhi;
import com.ruoyi.develop.lvFahuotongzhi.service.ILvFahuotongzhiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出货通知单Controller
 * @author qh
 * @date 2024-09-30
 */
@RestController
@RequestMapping("/lvFahuotongzhi/lvFahuotongzhi")
public class LvFahuotongzhiController extends BaseController {
    @Autowired
    private ILvFahuotongzhiService lvFahuotongzhiService;
    @Autowired
    private ILvFahuomingxiService fahuomingxiService;
    @Autowired
    private ILvYifahuomingxiService lvYifahuomingxiService;
    @Autowired
    private IGoodsSaleService iGoodsSaleService;
    /**
     * 查询出货通知单列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuotongzhi:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvFahuotongzhi entity) {
        startPage();
        List<LvFahuotongzhi> list = lvFahuotongzhiService.selectList(entity);
        return getDataTable(list);
    }

    @GetMapping("/listTz/{id}")
    public AjaxResult listTz(@PathVariable("id") String id) {
        return AjaxResult.success(lvFahuotongzhiService.selectById(id));
    }

    /**
     * 导出出货通知单列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuotongzhi:export')")
    @Log(title = "出货通知单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvFahuotongzhi entity) {
        List<LvFahuotongzhi> list = lvFahuotongzhiService.selectList(entity);
        ExcelUtil<LvFahuotongzhi> util = new ExcelUtil<LvFahuotongzhi>(LvFahuotongzhi. class);
        util.exportExcel(response, list, "出货通知单数据");
    }

    /**
     * 获取出货通知单详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuotongzhi:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvFahuotongzhiService.selectById(id));
    }

    /**
     * 新增出货通知单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuotongzhi:add')")
    @Log(title = "出货通知单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvFahuotongzhi entity) {
        entity.setState(BusinessConstants.FAHUO_1);
        entity.setStatus(BusinessConstants.FAHUO_1);
        lvFahuotongzhiService.insert(entity);
        fahuomingxiService.updateByFahuoId(entity.getId(),entity.getNos(),BusinessConstants.FAHUO_1);
        String projectNos="";
        List<LvFahuomingxi> lvFahuomingxiList=fahuomingxiService.selectByTogzhiIdGroupByProid(entity.getId());
        for(LvFahuomingxi lvFahuomingxi :lvFahuomingxiList){
            projectNos=projectNos+lvFahuomingxi.getProNo()+",";
        }
        if(lvFahuomingxiList.size()>0){
            entity.setSaleman(lvFahuomingxiList.get(0).getSaleman());
        }
        entity.setProjectNos(projectNos);
        return toAjax(lvFahuotongzhiService.update(entity));
    }

    /**
     * 修改出货通知单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuotongzhi:edit')")
    @Log(title = "出货通知单", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvFahuotongzhi entity) {
        return toAjax(lvFahuotongzhiService.update(entity));
    }

    /**
     * 删除出货通知单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuotongzhi:remove')")
    @Log(title = "出货通知单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<LvYifahuomingxi> lvYifahuomingxiList=lvYifahuomingxiService.selectByTogzhiId(ids[0]);
        if(lvYifahuomingxiList.size()>0) {
            return AjaxResult.error("该通知单下有已发货的产品，请弃审产品后删除！");
        }else{
            List<LvFahuomingxi> lvFahuomingxiList=fahuomingxiService.selectByTogzhiId(ids[0]);
            for(LvFahuomingxi lvFahuomingxi:lvFahuomingxiList){
                GoodsSale goodsSale=iGoodsSaleService.selectById(lvFahuomingxi.getSaleId());
                goodsSale.setIsfahuo(BusinessConstants.FAHUO_0);//未发货
                iGoodsSaleService.update(goodsSale);
            }
            fahuomingxiService.deleteByFahuoId(ids[0]);
            return toAjax(lvFahuotongzhiService.deleteByIds(ids));
        }
    }


    @PostMapping("/insertForTzd")
    public AjaxResult insertForTzd(@Validated @RequestBody LvFahuotongzhi entity) {
        lvFahuotongzhiService.insertBatch(entity);
        return AjaxResult.success("选择完成！");
    }
}
